<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>基本信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_SchemeName_FormItem"
								prop="Entity.SchemeName"
								:rules="[{ required: true, message: '方案名称为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="方案名称" tooltip="社保方案名称（如：一类、二类）" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.SchemeName" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SchemeCode_FormItem" prop="Entity.SchemeCode">
								<template #label>
									<WtmLabelTooltip label="方案编码" tooltip="社保方案编码" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.SchemeCode" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_PaymentArea_FormItem" prop="Entity.PaymentArea">
								<template #label>
									<WtmLabelTooltip label="缴纳地区" tooltip="社保缴纳所在地区" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.PaymentArea" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_IsEnabled_FormItem"
								prop="Entity.IsEnabled"
								:rules="[{ required: true, message: '是否启用为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="是否启用" tooltip="是否启用该方案" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.IsEnabled" clearable class="width100">
									<el-option :key="1" :value="true" :label="$t('message._system.common.vm.tips_bool_true')"></el-option>
									<el-option :key="0" :value="false" :label="$t('message._system.common.vm.tips_bool_false')"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>缴费信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SocialSecurityBase_FormItem" prop="Entity.SocialSecurityBase">
								<template #label>
									<WtmLabelTooltip label="社保缴费基数" tooltip="社保缴费基数" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.SocialSecurityBase" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>缴费明细</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_PensionPersonal_FormItem" prop="Entity.PensionPersonal">
								<template #label>
									<WtmLabelTooltip label="养老保险个人" tooltip="养老保险个人缴费" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.PensionPersonal" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_PensionCompany_FormItem" prop="Entity.PensionCompany">
								<template #label>
									<WtmLabelTooltip label="养老保险单位" tooltip="养老保险单位缴费" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.PensionCompany" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_MedicalPersonal_FormItem" prop="Entity.MedicalPersonal">
								<template #label>
									<WtmLabelTooltip label="医疗保险个人" tooltip="医疗保险个人缴费" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.MedicalPersonal" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_MedicalCompany_FormItem" prop="Entity.MedicalCompany">
								<template #label>
									<WtmLabelTooltip label="医疗保险单位" tooltip="医疗保险单位缴费" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.MedicalCompany" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_UnemploymentPersonal_FormItem" prop="Entity.UnemploymentPersonal">
								<template #label>
									<WtmLabelTooltip label="失业保险个人" tooltip="失业保险个人缴费" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.UnemploymentPersonal" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_UnemploymentCompany_FormItem" prop="Entity.UnemploymentCompany">
								<template #label>
									<WtmLabelTooltip label="失业保险单位" tooltip="失业保险单位缴费" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.UnemploymentCompany" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_InjuryCompany_FormItem" prop="Entity.InjuryCompany">
								<template #label>
									<WtmLabelTooltip label="工伤保险单位" tooltip="工伤保险单位缴费" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.InjuryCompany" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_MaternityCompany_FormItem" prop="Entity.MaternityCompany">
								<template #label>
									<WtmLabelTooltip label="生育保险单位" tooltip="生育保险单位缴费" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.MaternityCompany" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>缴费信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_HousingFundBase_FormItem" prop="Entity.HousingFundBase">
								<template #label>
									<WtmLabelTooltip label="公积金缴费基数" tooltip="公积金缴费基数" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.HousingFundBase" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>缴费明细</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_HousingFundPersonal_FormItem" prop="Entity.HousingFundPersonal">
								<template #label>
									<WtmLabelTooltip label="公积金个人" tooltip="公积金个人缴费" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.HousingFundPersonal" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_HousingFundCompany_FormItem" prop="Entity.HousingFundCompany">
								<template #label>
									<WtmLabelTooltip label="公积金单位" tooltip="公积金单位缴费" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.HousingFundCompany" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>统计信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_PersonalTotal_FormItem" prop="Entity.PersonalTotal">
								<template #label>
									<WtmLabelTooltip label="个人合计" tooltip="个人缴费合计" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.PersonalTotal" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_CompanyTotal_FormItem" prop="Entity.CompanyTotal">
								<template #label>
									<WtmLabelTooltip label="单位合计" tooltip="单位缴费合计" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.CompanyTotal" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>其他</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Remark_FormItem" prop="Entity.Remark">
								<template #label>
									<WtmLabelTooltip label="备注" tooltip="备注说明" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Remark" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<!--suppress JSAnnotator -->
<script setup lang="ts" name="message._system.common.vm.edit;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, computed, nextTick, ComponentInternalInstance } from 'vue';
import { SysPersonSocialSecuritySchemeApi as api } from '/@/views/person/syspersonsocialsecurityscheme/api';
import other from '/@/utils/other';
import { ISysPersonSocialSecuritySchemeVm } from '/@/views/person/syspersonsocialsecurityscheme/api/interface';
import { IViewModel } from '/@/types/common';

const BUTTON_TEXTS = {
	SAVE: '保存',
	SUBMIT: '提交',
	AGREE: '同意',
	REJECT: '拒绝',
	CLOSE: '关闭',
} as const;
const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const stateData = reactive<IViewModel<ISysPersonSocialSecuritySchemeVm>>({
	vmModel: {
		Entity: {
			ID: null,
			SchemeName: null,
			SchemeCode: null,
			PaymentArea: null,
			IsEnabled: false,
			SocialSecurityBase: null,
			PensionPersonal: null,
			PensionCompany: null,
			MedicalPersonal: null,
			MedicalCompany: null,
			UnemploymentPersonal: null,
			UnemploymentCompany: null,
			InjuryCompany: null,
			MaternityCompany: null,
			HousingFundBase: null,
			HousingFundPersonal: null,
			HousingFundCompany: null,
			PersonalTotal: null,
			CompanyTotal: null,
			Remark: null,
		},

		ActionName: null,
		IsMyApprove: null,
	},
});

const handleApiResponse = (response: any, isSubmit = false) => {
	ElMessage.success(ci.proxy?.$t('message._system.common.vm.submittip') || '提交成功');
	emit('refresh');
	loading.value = false;
	closeDialog();
};
// 统一的错误处理函数
const handleError = (error: any) => {
	other.setFormError(ci, error);
	loading.value = false;
};

// 公共的API请求处理函数
const handleApiRequest = (apiCall: () => Promise<any>, beforeSubmit: () => void, isSubmit = false) => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			beforeSubmit();
			apiCall()
				.then((response: any) => handleApiResponse(response, isSubmit))
				.catch(handleError);
		}
	});
};

// 取消
const onClose = () => {
	closeDialog();
};

// 保存
const onSave = () => {
	handleApiRequest(
		() => api().edit(stateData.vmModel),
		() => {},
		false
	);
};

// 加载选择列表数据
const loadData = async () => {
	await Promise.allSettled([
		api()
			.get(stateData.vmModel.Entity.ID ?? '')
			.then((x) => other.setValue(stateData.vmModel, x)),
	]);
};

// 初始化页面数据
const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
	}
};
// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		// 并行加载选择列表数据
		await loadData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.SUBMIT,
			type: 'primary',
			onClick: onSave,
			visible: true,
		},
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];
	emit('updateHeaderButtons', headerButtons);
};
</script>
